﻿using System;
using GeoAPI.Geometries;
using HYCodeBase.Geometry;
using NetTopologySuite.Geometries;

namespace HYCodeBase.TopologySuite
{
	public class HYPolygonRelateOperater : BaseRelateOperater
	{
		public bool TryGetGeometryRelate(HYPolygon inputPolygonOne, HYPolygon inputPolygonTwo, out IntersectionMatrix relateResult)
		{
			relateResult = null;
			if (inputPolygonOne == null || inputPolygonTwo == null)
			{
				return false;
			}
			NtsGeometryGeter ntsGeometryGeter = NtsGeometryGeter.GetNtsGeometryGeter();
			Polygon inputGeometryOne = null;
			Polygon inputGemetryTwo = null;
			return ntsGeometryGeter.TryMakePolygon(inputPolygonOne, out inputGeometryOne, true) && ntsGeometryGeter.TryMakePolygon(inputPolygonTwo, out inputGemetryTwo, true) && base.TryGetGeometryRelate(inputGeometryOne, inputGemetryTwo, out relateResult);
		}

		public bool TryGetGeometryRelate2(HYPolygon inputPolygonOne, HYPolygon inputPolygonTwo, out TopologyRelateKindEnum relateResult)
		{
			relateResult = TopologyRelateKindEnum.Disjoint;
			IntersectionMatrix intersectionMatrix = null;
			if (!this.TryGetGeometryRelate(inputPolygonOne, inputPolygonTwo, out intersectionMatrix) || intersectionMatrix == null)
			{
				return false;
			}
			bool result;
			try
			{
				if (intersectionMatrix.IsDisjoint())
				{
					relateResult = TopologyRelateKindEnum.Disjoint;
				}
				else if (intersectionMatrix.IsTouches(Dimension.Curve, Dimension.Curve))
				{
					relateResult = TopologyRelateKindEnum.Touches;
				}
				else if (intersectionMatrix.IsEquals(Dimension.Curve, Dimension.Curve))
				{
					relateResult = TopologyRelateKindEnum.Equal;
				}
				else if (intersectionMatrix.IsWithin())
				{
					relateResult = TopologyRelateKindEnum.Within;
				}
				else if (intersectionMatrix.IsContains())
				{
					relateResult = TopologyRelateKindEnum.Contains;
				}
				else if (intersectionMatrix.IsOverlaps(Dimension.Surface, Dimension.Surface))
				{
					relateResult = TopologyRelateKindEnum.Overlaps;
				}
				else
				{
					relateResult = TopologyRelateKindEnum.Intersection;
				}
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}
	}
}
